<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
            <a-row :gutter="24">
                <a-col :span="6">
                    <a-form-item label="班级名称" name="bjmc">
                        <a-input v-model:value="searchFormState.bjmc" placeholder="请输入班级名称" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="学年学期" name="xnxq01id">
                        <a-input v-model:value="searchFormState.xnxq01id" placeholder="请输入学年学期" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="教工号" name="jszgh">
                        <a-input v-model:value="searchFormState.jszgh" placeholder="请输入教工号" />
                    </a-form-item>
                </a-col>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="教师姓名" name="jsxm">
                            <a-input v-model:value="searchFormState.jsxm" placeholder="请输入教师姓名" />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="课程专业名称" name="kczwmc">
                            <a-input v-model:value="searchFormState.kczwmc" placeholder="请输入课程专业名称" />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="课堂名称" name="ktmc">
                            <a-input v-model:value="searchFormState.ktmc" placeholder="请输入课堂名称" />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="实践学时" name="sjxs">
                            <a-input v-model:value="searchFormState.sjxs" placeholder="请输入实践学时" />
                        </a-form-item>
                    </a-col>
                </template>
                <a-col :span="6">
                    <a-button type="primary" @click="table.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                        {{ advanced ? '收起' : '展开' }}
                        <component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
                    </a>
                </a-col>
            </a-row>
        </a-form>
        <s-table
            ref="table"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.kkdm"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
           <a-space>
                <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('jwXkxxAdd')">
                    <template #icon><plus-outlined /></template>
                    新增
                </a-button>
                <xn-batch-button
                    v-if="hasPerm('jwXkxxBatchDelete')"
                    buttonName="批量删除"
                       icon="DeleteOutlined"
                    :selectedRowKeys="selectedRowKeys"
                    @batchCallBack="deleteBatchJwXkxx"
                />
            </a-space>
            </template>
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)" v-if="hasPerm('jwXkxxEdit')">编辑</a>
                        <a-divider type="vertical" v-if="hasPerm(['jwXkxxEdit', 'jwXkxxDelete'], 'and')" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteJwXkxx(record)">
                            <a-button type="link" danger size="small" v-if="hasPerm('jwXkxxDelete')">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="xkxx">
    import { message } from 'ant-design-vue'
    import Form from './form.vue'
    import jwXkxxApi from '@/api/biz/jwXkxxApi'
    let searchFormState = reactive({})
    const searchFormRef = ref()
    const table = ref()
    const formRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
    // 查询区域显示更多控制
    const advanced = ref(false)
    const toggleAdvanced = () => {
        advanced.value = !advanced.value
    }
    const columns = [
        {
            title: '学号',
            dataIndex: 'xh'
        },
        {
            title: '学生姓名',
            dataIndex: 'xm'
        },
        {
            title: '班级代码',
            dataIndex: 'bjdm'
        },
        {
            title: '班级名称',
            dataIndex: 'bjmc'
        },
        {
            title: '学年学期',
            dataIndex: 'xnxq01id'
        },
        {
            title: '学年',
            dataIndex: 'xn'
        },
        {
            title: '学期',
            dataIndex: 'xq'
        },
        {
            title: '教工号',
            dataIndex: 'jszgh'
        },
        {
            title: '教师姓名',
            dataIndex: 'jsxm'
        },
        {
            title: '课程代码',
            dataIndex: 'kcdm'
        },
        {
            title: '课程专业名称',
            dataIndex: 'kczwmc'
        },
        {
            title: '星期几',
            dataIndex: 'xqj'
        },
        {
            title: '时间段',
            dataIndex: 'ddj'
        },
        {
            title: '时间段序号',
            dataIndex: 'sjdxh'
        },
        {
            title: '结束时间序号',
            dataIndex: 'jssjxh'
        },
        {
            title: '教室编号',
            dataIndex: 'jsbh'
        },
        {
            title: '教室名称',
            dataIndex: 'jsmc'
        },
        {
            title: '教学楼',
            dataIndex: 'lh'
        },
        {
            title: '起始周',
            dataIndex: 'qsz'
        },
        {
            title: '结束周',
            dataIndex: 'jsz'
        },
        {
            title: '单双周',
            dataIndex: 'dsz'
        },
        {
            title: '课表ID',
            dataIndex: 'jx0408id'
        },
        {
            title: '课堂名称',
            dataIndex: 'ktmc'
        },
        {
            title: '教学班号',
            dataIndex: 'xx04id'
        },
        {
            title: '教师类型码',
            dataIndex: 'jslxm'
        },
        {
            title: '教师类型',
            dataIndex: 'jslx'
        },
        {
            title: '总学时',
            dataIndex: 'zxs'
        },
        {
            title: '学分',
            dataIndex: 'xf'
        },
        {
            title: '课程通识名称',
            dataIndex: 'kctxmc'
        },
        {
            title: '课程属性名称',
            dataIndex: 'kcsxmc'
        },
        {
            title: '课程性质名称',
            dataIndex: 'kcxzmc'
        },
        {
            title: '课程辅主类型',
            dataIndex: 'kcfalx'
        },
        {
            title: '上课教师职称',
            dataIndex: 'skjszc'
        },
        {
            title: '选课方式',
            dataIndex: 'xkfs'
        },
        {
            title: '考试方式代码',
            dataIndex: 'ksfsdm'
        },
        {
            title: '考试方式名称',
            dataIndex: 'ksfsmc'
        },
        {
            title: '上课校区代码',
            dataIndex: 'skxqdm'
        },
        {
            title: '上课校区名称',
            dataIndex: 'skxqmc'
        },
        {
            title: '理论学时',
            dataIndex: 'llxs'
        },
        {
            title: '实践学时',
            dataIndex: 'sjxs'
        },
        {
            title: '实验学时',
            dataIndex: 'syxs'
        },
        {
            title: '其它学时',
            dataIndex: 'qtxs'
        },
    ]
    // 操作栏通过权限判断是否显示
    if (hasPerm(['jwXkxxEdit', 'jwXkxxDelete'])) {
        columns.push({
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: '150px'
        })
    }
    let selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        alert: {
            show: false,
            clear: () => {
                selectedRowKeys = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
        const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
        return jwXkxxApi.jwXkxxPage(Object.assign(parameter, searchFormParam)).then((data) => {
            return data
        })
    }
    // 删除
    const deleteJwXkxx = (record) => {
        let params = [
            {
                kkdm: record.kkdm
            }
        ]
        jwXkxxApi.jwXkxxDelete(params).then(() => {
            table.value.refresh(true)
        })
    }
    // 批量删除
    const deleteBatchJwXkxx = () => {
        if (selectedRowKeys.value.length < 1) {
            message.warning('请选择一条或多条数据')
            return false
        }
        const params = selectedRowKeys.value.map((m) => {
            return {
                kkdm: m
            }
        })
        jwXkxxApi.jwXkxxDelete(params).then(() => {
            table.value.clearRefreshSelected()
        })
    }
</script>
